package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;

import com.qidianit.mes.pojo.pro.WorkflowProduct;
import com.qidianit.mes.pro.service.WorkflowProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/workflowProduct")
@CrossOrigin
public class WorkflowProductController {

    @Autowired
    private WorkflowProductService workflowProductService;
    private String getCurrentUser(HttpServletRequest request) {
        return "admin";
    }


    @GetMapping("/page")
    public Result<PageInfo<WorkflowProduct>> getPage(
            @RequestParam Long workflowId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        System.out.println("分页查询");
        return workflowProductService.selectPage(workflowId, page, size);
    }

    @GetMapping("/{id}")
    public Result<WorkflowProduct> getById(@PathVariable("id") Long id) {
        return workflowProductService.selectById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody WorkflowProduct workflowProduct, HttpServletRequest request) {
        workflowProduct.setCreateBy(getCurrentUser(request));
        return workflowProductService.add(workflowProduct);
    }

    @PutMapping("/update")
    public Result update(@RequestBody WorkflowProduct workflowProduct, HttpServletRequest request) {
        workflowProduct.setUpdateBy(getCurrentUser(request));
        return workflowProductService.update(workflowProduct);
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return workflowProductService.delete(id);
    }

    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestParam Long[] id) {
        return workflowProductService.batchDelete(id);
    }


}
